const fs = require("fs");

function loadData(filename = "./2kt_flow_record.json") {
  console.log(stamp() + "读取存档...");
  let loadingTime = new Date().getTime();
  return new Promise((resolve, reject) => {
    fs.readFile(filename, "utf8", (err, file) => {
      if (err) {
        console.error("读取文件时出错:", err);
        // return null;
        reject(null);
      }
      try {
        const content = JSON.parse(file);
        loadingTime = new Date().getTime() - loadingTime;
        console.log(`${stamp()}已读取, 用时${loadingTime}ms`);
        resolve(content);
      } catch (e) {
        console.error("解析JSON时出错:", e);
        reject(null);
      }
    });
  });
}

function stamp() {
  let dt = new Date();
  return `[${dt.getMinutes()}:${dt.getSeconds()}.${dt.getMilliseconds()}]`;
}

module.exports = {
  loadData,
  stamp,
};
